//logs.js
//获取应用实例
const app = getApp()
const util = require('../../utils/util.js')

Page({
  data: {
    winHeight: 0,
    list_height: 0, //list容器高度
    userId: '',
    lodding: false,
    noData: false,
    noOneData: false,
    balance: '0.00',
    balanceList: [],
  },
  onLoad: function () {
    var that = this
    that.setData({
      winHeight: app.globalData.winHeight,
      userId: app.globalData.wxUserId
    })
    that.getBalanceList()
  },
  onShow: function () {
    this.getBalance()
  },
  onReachBottom: function() { //上拉刷新
    var that = this
    that.getBalanceList()
  },
  onPullDownRefresh: function() { //下拉刷新
    var that = this
    console.log("下拉刷新")
  },
  getBalanceList(){
    var that = this
    if(!that.data.lodding && !that.data.noData){
      console.log("上拉刷新")
      that.showLodding()
      var offset = that.data.balanceList.length
      var data = {
        userId: that.data.userId,
        offset: offset,
      }
      app.http.post('/wechat/getBalanceList',data).then(res =>{
        let noData = false
        let noOneData = false
        if(res.status == '200'){
          var listData = res.data
          if(listData.length < 5){ // 获取数据数 小于分页数则表示无数据了
            noData = true
          }
          listData = that.data.balanceList.concat(listData)
          if(listData.length == 0){ //获取总长度为0 则展示背景图
            noOneData = true
          }
          that.setData({
            balanceList: listData,
            lodding: false,
            noData: noData,
            noOneData: noOneData,
            list_height: listData.length * 70
          })
          
        } else {
          that.setData({
            lodding: false,
            noData: noData,
          })
        }
      })


    }
  },
  getBalance: function() {
    var that = this
    var data = {
      userId: that.data.userId
    }
    app.http.post('/wechat/getBalance',data).then(res =>{
      if(res.status == '200'){
        that.setData({
          balance: res.data
        })
      }
    })

  },
  showLodding: function() { // 显示列表数据加载动画
    var that = this
    if(!that.data.lodding) {
      that.setData({
        lodding: true,
        noData: false
      })
    }
  },
  hideLodding: function() { //隐藏列表数据加载动画
    that.setData({
      lodding: false
    })
  },
})
